package com.atguigu.gulimall.product.app;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.common.vilid.AddGroup;
import com.atguigu.common.vilid.UpdateGroup;
import com.atguigu.common.vilid.UpdateStatusGroup;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 品牌
 *
 * @author yuwenyi
 * @email 1171370471@qq.com
 * @date 2021-05-25 22:31:54
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated(value = {AddGroup.class})   //将@Valid改为spring带的注解
                      @RequestBody BrandEntity brand/*, BindingResult result*/){
/*        if (result.hasErrors()){
            Map<String,String> map = new HashMap<>();
            result.getFieldErrors().forEach((field)->{
                String message = field.getDefaultMessage();
                String fieldName = field.getField();
                map.put(fieldName, message);
            });
            return R.error(400,"提交的数据不合法!").put("data", map);
        }else {*/
        //异常处理交给统一异常控制中心
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody BrandEntity brand){
		//由于有关联关系,因此不能只更新品牌表,所有有关联关系的表有需要同步更新,这也是没有设置外键之后必须要做的事
		//brandService.updateById(brand);

		brandService.updateDetail(brand);

        return R.ok();
    }

    //专门用来修改状态
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated(value = {UpdateStatusGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
